package com.controller;


import com.domain.Course;
import com.domain.CourseLesson;
import com.domain.CourseSection;
import com.domain.ResponseResult;
import com.service.CourseContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/courseContent")
public class CourseContentController {

    @Autowired
    private CourseContentService courseContentService;

    /*
     * 根据课程id查询关联的章节信息及其关联的课时信息
     * */
    @RequestMapping("/findSectionAndLesson")
    public ResponseResult findSectionAndLesson(Integer courseId){
        List<CourseSection> sections = courseContentService.findSectionAndLessonByCourseId(courseId);
        return new ResponseResult(true,200,"响应成功",sections);
    }
    /*
     * 回显章节对应的课程信息
     * */
    @RequestMapping("/findCourseByCourseId")
    public ResponseResult findCourseByCourseId(Integer courseId){
        Course course = courseContentService.findCourseByCourseId(courseId);
        return new ResponseResult(true,200,"查询课程信息成功",course);
    }
    /*
     * 新建及修改章节
     * */
    @RequestMapping("/saveOrUpdateSection")
    public ResponseResult saveOrUpdateSection(@RequestBody CourseSection courseSection){
        if (courseSection.getId() == null){
            courseContentService.saveSection(courseSection);
            return new ResponseResult(true,200,"添加章节成功",null);
        }else {
            courseContentService.updateSection(courseSection);
            return new ResponseResult(true,200,"修改章节成功",null);
        }
    }
    /*
     * 修改章节状态
     * */
    @RequestMapping("/updateSectionStatus")
    public ResponseResult updateSectionStatus(int id,int status){
        courseContentService.updateSectionStatus(id,status);
        Map<String, Object> map = new HashMap<>();
        map.put("status",status);
        return new ResponseResult(true,200,"修改状态成功",map);
    }
    /*
     * 新建及修改课时
     * */
    @RequestMapping("/saveOrUpdateLesson")
    public ResponseResult saveOrUpdateLesson(@RequestBody CourseLesson courseLesson){
        if (courseLesson.getId() == null){
            courseContentService.saveLesson(courseLesson);
            return new ResponseResult(true,200,"添加课时成功",null);
        }else {
            courseContentService.updateLesson(courseLesson);
            return new ResponseResult(true,200,"修改课时成功",null);
        }
    }

}
